﻿using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static OrderService orderService = new OrderService();

    static void Main()
    {
        while (true)
        {
            Console.WriteLine("\n订单管理系统：");
            Console.WriteLine("1. 添加订单");
            Console.WriteLine("2. 删除订单");
            Console.WriteLine("3. 修改订单");
            Console.WriteLine("4. 查询订单");
            Console.WriteLine("5. 退出");
            Console.Write("请选择操作：");

            string operation = Console.ReadLine();
            switch (operation)
            {
                case "1":
                    AddOrder();
                    break;
                case "2":
                    RemoveOrder();
                    break;
                case "3":
                    ModifyOrder();
                    break;
                case "4":
                    QueryOrder();
                    break;
                case "5":
                    return;
                default:
                    Console.WriteLine("未知操作，请重试。");
                    break;
            }
        }
    }

    static void AddOrder()
    {
        try
        {
            Console.Write("请输入订单号: ");
            int orderId = int.Parse(Console.ReadLine());
            Console.Write("请输入客户名: ");
            string customer = Console.ReadLine();

            Order order = new Order { OrderId = orderId, Customer = customer };
            Console.WriteLine("请输入订单明细，格式为 '商品名称 单价 数量'，输入 '完成' 结束添加：");

            string input;
            while ((input = Console.ReadLine()) != "完成")
            {
                string[] parts = input.Split();
                if (parts.Length != 3)
                {
                    Console.WriteLine("输入格式错误，请重新输入。");
                    continue;
                }

                string productName = parts[0];
                if (order.OrderDetails.Any(d => d.ProductName == productName))
                {
                    Console.WriteLine("订单明细中商品名称重复，请重新输入。");
                    continue;
                }

                if (!decimal.TryParse(parts[1], out decimal unitPrice))
                {
                    Console.WriteLine("单价应为数字，请重新输入。");
                    continue;
                }

                if (!int.TryParse(parts[2], out int quantity))
                {
                    Console.WriteLine("数量应为整数，请重新输入。");
                    continue;
                }

                order.OrderDetails.Add(new OrderDetail { ProductName = productName, UnitPrice = unitPrice, Quantity = quantity });
            }

            orderService.AddOrder(order);
            Console.WriteLine("订单已添加。");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误: {ex.Message}");
        }
    }

    static void RemoveOrder()
    {
        try
        {
            Console.Write("请输入要删除的订单号: ");
            int orderId = int.Parse(Console.ReadLine());
            orderService.RemoveOrder(orderId);

            Console.WriteLine("订单已删除。");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误: {ex.Message}");
        }
    }

    static void ModifyOrder()
    {
        try
        {
            Console.Write("请输入要修改的订单号: ");
            int orderId = int.Parse(Console.ReadLine());
            var existingOrder = orderService.QueryOrders(o => o.OrderId == orderId).FirstOrDefault();
            if (existingOrder == null)
            {
                Console.WriteLine("订单不存在，无法修改。");
                return;
            }

            Console.Write("请输入新的客户名（留空表示不修改）: ");
            string customer = Console.ReadLine();
            if (!string.IsNullOrEmpty(customer))
            {
                existingOrder.Customer = customer;
            }

            Console.WriteLine("如需添加订单明细，请输入，格式为 '商品名称 单价 数量'，输入 '完成' 结束添加：");
            string input;
            while ((input = Console.ReadLine()) != "完成")
            {
                string[] parts = input.Split();
                if (parts.Length != 3)
                {
                    Console.WriteLine("输入格式错误，请重新输入。");
                    continue;
                }

                string productName = parts[0];
                if (existingOrder.OrderDetails.Any(d => d.ProductName == productName))
                {
                    Console.WriteLine("订单明细中商品名称重复，请重新输入。");
                    continue;
                }

                if (!decimal.TryParse(parts[1], out decimal unitPrice))
                {
                    Console.WriteLine("单价应为数字，请重新输入。");
                    continue;
                }

                if (!int.TryParse(parts[2], out int quantity))
                {
                    Console.WriteLine("数量应为整数，请重新输入。");
                    continue;
                }

                existingOrder.OrderDetails.Add(new OrderDetail { ProductName = productName, UnitPrice = unitPrice, Quantity = quantity });
            }

            orderService.ModifyOrder(existingOrder);
            Console.WriteLine("订单已修改。");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误: {ex.Message}");
        }
    }

    static void QueryOrder()
    {
        try
        {
            Console.WriteLine("请选择查询方式：");
            Console.WriteLine("1. 按订单号查询");
            Console.WriteLine("2. 按商品名称查询");
            Console.WriteLine("3. 按客户查询");
            Console.WriteLine("4. 按订单金额查询");
            Console.Write("请选择操作：");

            string queryType = Console.ReadLine();
            switch (queryType)
            {
                case "1":
                    QueryByOrderId();
                    break;
                case "2":
                    QueryByProductName();
                    break;
                case "3":
                    QueryByCustomer();
                    break;
                case "4":
                    QueryByAmount();
                    break;
                default:
                    Console.WriteLine("未知操作，请重试。");
                    break;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误: {ex.Message}");
        }
    }

    static void QueryByOrderId()
    {
        Console.Write("请输入订单号: ");
        int orderId = int.Parse(Console.ReadLine());
        var order = orderService.QueryOrders(o => o.OrderId == orderId).FirstOrDefault();
        if (order != null)
        {
            Console.WriteLine(order);
        }
        else
        {
            Console.WriteLine("未找到该订单号的订单。");
        }
    }

    static void QueryByProductName()
    {
        Console.Write("请输入商品名称: ");
        string productName = Console.ReadLine();
        var orders = orderService.QueryOrders(o => o.OrderDetails.Any(d => d.ProductName == productName));
        if (orders.Any())
        {
            foreach (var order in orders)
            {
                Console.WriteLine(order);
            }
        }
        else
        {
            Console.WriteLine("未找到包含该商品名称的订单。");
        }
    }

    static void QueryByCustomer()
    {
        Console.Write("请输入客户名称: ");
        string customer = Console.ReadLine();
        var orders = orderService.QueryOrders(o => o.Customer == customer);
        if (orders.Any())
        {
            foreach (var order in orders)
            {
                Console.WriteLine(order);
            }
        }
        else
        {
            Console.WriteLine("未找到该客户的订单。");
        }
    }

    static void QueryByAmount()
    {
        Console.Write("请输入订单金额: ");
        decimal amount = decimal.Parse(Console.ReadLine());
        var orders = orderService.QueryOrders(o => o.TotalAmount == amount);
        if (orders.Any())
        {
            foreach (var order in orders)
            {
                Console.WriteLine(order);
            }
        }
        else
        {
            Console.WriteLine("未找到该金额的订单。");
        }
    }
}
